/**
 * <b>项目名：</b>hclv-web<br/>  
 * <b>包名：</b>com.flea.hclv.controller<br/>  
 * <b>文件名：</b>HHotelController.java<br/>  
 * <b>版本信息：</b>1.0<br/>  
 * <b>日期：</b>2018年9月12日 下午3:29:31<br/>  
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.entity.HHotel;
import com.flea.hclv.model.entity.HLine;
import com.flea.hclv.model.entity.vo.LineSpotRelation;
import com.flea.hclv.service.HLineService;
import com.flea.hclv.service.HLineSpotRelationService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.ControllerResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description TODO
 * @createTime 2018年9月12日 下午3:29:31
 * @modifyTime
 * @author myflea@163.com
 * @version 1.0
 */
@Controller
@RequestMapping("/flea/hclv/line/")
public class HLineController {
	@Autowired
	private HLineService lineService;

	@Autowired
	private HLineSpotRelationService lineSpotRelationService;

	Logger logger = LoggerFactory.getLogger(HLineController.class);

	@RequestMapping("/queryAllForPage")
	public void queryAll(HttpServletRequest request, FleaPage<HHotel> paging, HttpServletResponse response,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "type", required = false) String type) {
		FleaPage<HLine> pageList = lineService.findForGird(paging.getCurPage(), paging.getPageSize(), name, type,
				queryDeptId(FleaOrgUtils.getCurUserUuid(request)));
		ControllerResult conresult = new ControllerResult(pageList);
		conresult.set(true, "操作成功");

		conresult.WritePagingDataToPage(response, pageList);
	}

	@RequestMapping("/queryAll")
	public void queryAll(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "type", required = false) String type) {
		List<HLine> lines = lineService.queryAll(queryDeptId(FleaOrgUtils.getCurUserUuid(request)), type);
		ControllerResult conresult = new ControllerResult(true, "操作成功", lines);
		conresult.WriteToPage(response);
	}

	@RequestMapping("/querySpotsForLine")
	public void querySpotsForLine(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "lineId", required = false) String lineId) {
		List<LineSpotRelation> lineSpotRelation = lineSpotRelationService.queryByLineId(lineId);
		ControllerResult conresult = new ControllerResult(true, "操作成功", lineSpotRelation);
		conresult.WriteToPage(response);
	}

	@RequestMapping("/detail")
	public void detail(HttpServletRequest request, HttpServletResponse response, @Param("id") String id) {
		HLine hLine = lineService.queryById(id);
		ControllerResult conresult = new ControllerResult(true, "操作成功", hLine);
		conresult.WriteToPage(response);
	}

	@RequestMapping("save")
	public void save(HttpServletRequest request,
			HttpServletResponse response/* , @RequestParam(value="lines", required = false) String lines */) {
		String productJSON = request.getParameter("model");
		HLine hLine = JSON.parseObject(productJSON, HLine.class);
		String userId = FleaOrgUtils.getCurUserUuid(request);
		hLine.setCreateUser(userId);
		hLine.setUuid(queryDeptId(userId));
		JSONObject jsonObject = JSON.parseObject(productJSON);
		String spots = jsonObject.getString("spots");

		boolean result = true;
		try {
			result = lineService.save(hLine, spots);
		} catch (Exception e) {
      ControllerResult cResult = new ControllerResult(false, e.getMessage(), result);
      cResult.WriteToPage(response);
		}
		ControllerResult cResult = new ControllerResult(true, "操作成功", result);
		cResult.WriteToPage(response);
	}

  /**
   * 景点修改 业务描述  1、基本信息维护 2、景点关系维护 3、先删除关联关系
   * model :baseInfo , spot
   * @param request
   * @param response
   */
  @ResponseBody
	@RequestMapping("update")
	public ServerResponse<HLine> updateHotel(HttpServletRequest request, HttpServletResponse response) {
		String lineInfos = request.getParameter("model");
		if(null==lineInfos){
		  return ServerResponse.createByErrorMessage("参数为空，请求非法");
    }
    ServerResponse<HLine> result = null;
    try {
      result = lineService.updateInfo(request,lineInfos);
      return result;
    } catch (Exception e) {
      //e.printStackTrace();
      return ServerResponse.createByErrorMessage(e.getMessage());
    }

		/*HLine hLine = JSON.parseObject(lineInfos, HLine.class);
		hLine.setModifyUser(FleaOrgUtils.getCurUserUuid(request));

		JSONObject jsonObject = JSON.parseObject(lineInfos);
		String lines = jsonObject.getString("lines");

		boolean result = true;
		try {
			result = lineService.update(hLine, lines);
		} catch (Exception e) {
			// TODO: handle exception
		}
		ControllerResult cResult = new ControllerResult(true, "操作成功", result);
		cResult.WriteToPage(response);*/
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request, HttpServletResponse response, @Param("ids") String ids) {
		boolean result = true;
		String userId = FleaOrgUtils.getCurUserUuid(request);
		try {
			result = lineService.delete(ids, userId);
		} catch (Exception e) {
			// TODO: handle exception
		}
		ControllerResult cResult = new ControllerResult(true, "操作成功", result);
		cResult.WriteToPage(response);
	}

	public String queryDeptId(String userId) {
		OrgDept dept = FleaOrgUtils.getCurrentUserDept(userId);
		String deptId = HConstant.HCLV_ADMIN_ADMIN;
		if (dept != null) {
			deptId = dept.getDeptUuid();
		}
		return deptId;
	}
}
